package com.cogent.business.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.cogent.business.action.AbstractAction;
import com.cogent.business.cache.UserCacheManager;
import com.cogent.business.helper.UserDomainHelper;
import com.cogent.business.helper.ValidatorHelper;
import com.cogent.common.util.Globals;
import com.cogent.common.util.MD5;
import com.cogent.common.util.MapUtil;

import com.cogent.dao.domain.Pagination;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.type.QueryType;
import com.opensymphony.oscache.util.StringUtil;

public class SysUserAction extends AbstractAction {
	private static final String RESOURCE_CODE = "SYS_USER";
	private final static String REC_RESOURCE_CODE = "SYS_USER_REC";
	
	public String list() {
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.VIEW))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("organcode", MapUtil.getStringValue(formMap, "organcode"), QueryType.LEFTMATCH));				
		queryList.add(new QueryParamDomain("name", MapUtil.getStringValue(formMap, "name"), QueryType.LEFTMATCH));
		queryList.add(new QueryParamDomain("username", MapUtil.getStringValue(formMap, "username"), QueryType.LEFTMATCH));
		Pagination pagination = bussinessFacade.queryPaged("admin", queryList, "", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "list";
	}
	
	public String add() {
		HashMap formMap = getFormMapWithUser();
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.SAVE))
			return "noright";
		setResponseMsg("formmap", formMap);
		return "add";
	}
	
	public String mod() {
		HashMap formMap = getFormMapWithUser();
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.SAVE))
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("admin", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);
		
		return "mod";
	}
	
	public String view() {
		HashMap formMap = getFormMapWithUser();
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.VIEW)) && (!UserDomainHelper.isAllow(ServletActionContext.getRequest(), REC_RESOURCE_CODE, Globals.VIEW)))
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID("admin", queryList);
		setResponseMsg("datamap", dataMap);
		setResponseMsg("formmap", formMap);
		
		return "view";
	}
	
	public String save() {
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.SAVE)) && (!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.UPDATE)))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		boolean isAdd = MapUtil.isEmpty(formMap, "id");
		List curQueryList = new ArrayList();
		curQueryList.add(new QueryParamDomain("username",MapUtil.getStringValue(formMap, "username"),QueryType.EQUAL));
		HashMap curdataMap = bussinessFacade.getOneRowByTable("admin", curQueryList);
		if (MapUtil.getStringValue(curdataMap, "id") != null && !MapUtil.getStringValue(curdataMap, "id").equals(MapUtil.getStringValue(formMap, "id"))) {
			setNoteMsg("SysUserForm", "用户名存在重复！");
			setSessionInfo("formmap", formMap);
			if (isAdd) {
				setResponseUrl("SysUser_add.action");
			} else {
				setResponseUrl("SysUser_mod.action?id="
						+ MapUtil.getStringValue(formMap, "id"));
			}
			return null;
		}
		
		
		
		String pwd = MapUtil.getStringValue(formMap, "password");		
		if(!isAdd)
			formMap.put("password", "123");
		String errmsg = ValidatorHelper.validator("SysUserForm", formMap);
		if(!StringUtil.isEmpty(errmsg)) {
			setNoteMsg("SysUserForm", errmsg);
			setSessionInfo("formmap", formMap);
			setResponseUrl(formMap, "SysUser_mod.action");
			return null;
		}
		if(!isAdd)
			formMap.remove("password");
		else {
			MD5 md5 = new MD5();
			formMap.put("password", md5.getMD5ofStr(pwd));
		}
		bussinessFacade.saveOrUpdate("admin", formMap);
		UserCacheManager.remove(MapUtil.getStringValue(formMap, "id"));
		setResponseUrl(formMap, "SysUser_list.action");
		
		return null;
	}
	
	public String reset() {
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), RESOURCE_CODE, Globals.UPDATE))
			return "noright";
		HashMap formMap = getFormMapWithUser();
		HashMap dataMap = new HashMap();
		dataMap.put("id", MapUtil.getStringValue(formMap, "id"));
		dataMap.put("modifier", MapUtil.getStringValue(formMap, "modifier"));
		dataMap.put("modifyDate", MapUtil.getStringValue(formMap, "modifydate"));
		MD5 md5 = new MD5();
		dataMap.put("password", md5.getMD5ofStr("666666"));
		bussinessFacade.update("admin", dataMap);
		setResponseUrl(formMap, "SysUser_list.action");
		
		return null;
	}
	
	
	
	
}
